package com.china_goldcard.gridpagemenu;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jk on 16/10/22.
 */

public class GridFragment<T extends  GridMenuModel> extends Fragment {

    public interface OnGridItemClickListener<T extends GridMenuModel>{
        void onItemClicked(T item);
        void onItemLongClicked(T item);
    }

    private GridMenuDataAdapter<T> dataAdapter;

    private OnGridItemClickListener gridItemClickListener;

    private View view;

    private Context context;

    private GridView gridView;

    private List<T> dataList = new ArrayList<>();

    private final static String DATA_KEY = "data";

    /**
     * newInstance方法
     * @param data
     * @return
     */
    public static <T extends GridMenuModel>GridFragment newInstance(GridItemData<T> data){
        GridFragment gridFragment = new GridFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(DATA_KEY,data);
        gridFragment.setArguments(bundle);
        return gridFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            context = getActivity();
            Bundle args = getArguments();
            GridItemData<T> data = (GridItemData<T>)args.getSerializable(DATA_KEY);
            view = inflater.inflate(R.layout.grid_menu_fragment_view,container,false);
            gridView = (GridView) view.findViewById(R.id.fragment_grid_view);
            gridView.setNumColumns(data.getPageSize());
            List<T> newDataList = new ArrayList<>();
            int lastIndex = data.getPageNo() * data.getPageSize() * (data.getcPageNo() + 1);
            if (lastIndex >= data.getDataList().size()){
                newDataList = data.getDataList().subList(data.getPageNo() * data.getPageSize() * data.getcPageNo(),data.getDataList().size());
            }else{
                newDataList = data.getDataList().subList(data.getPageNo() * data.getPageSize() * data.getcPageNo(),lastIndex);
            }
            dataList.addAll(newDataList);
            GridAdapter<T> gridAdapter = new GridAdapter(context,dataList,R.layout.grid_menu_fragment_item_view,data.getItemH());
            gridAdapter.setDataAdapter(dataAdapter);
            gridView.setAdapter(gridAdapter);
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (gridItemClickListener != null){
                        gridItemClickListener.onItemClicked(dataList.get(position));
                    }
                }
            });
            gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    if (gridItemClickListener != null){
                        gridItemClickListener.onItemLongClicked(dataList.get(position));
                    }
                    return false;
                }
            });
        }else{
            ViewGroup root = (ViewGroup) view.getParent();
            if (root != null) {
                root.removeView(view);
            }
        }
        return view;
    }

    public OnGridItemClickListener getGridItemClickListener() {
        return gridItemClickListener;
    }

    public void setGridItemClickListener(OnGridItemClickListener gridItemClickListener) {
        this.gridItemClickListener = gridItemClickListener;
    }

    public GridMenuDataAdapter<T> getDataAdapter() {
        return dataAdapter;
    }

    public void setDataAdapter(GridMenuDataAdapter<T> dataAdapter) {
        this.dataAdapter = dataAdapter;
    }
}
